﻿function resetGridHeight(grid) {
    grid.setHeight($(".l-layout-center").height() - $("#maingrid").position().top - 30);
};

String.prototype.trim = function () {
    var str = this.replace(/^(\s|\u00A0)+/, '');
    for (var i = str.length - 1; i >= 0; i--) {
        if (/\S/.test(str.charAt(i))) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    return str;
};


String.prototype.isNum = function () {
    return !isNaN(this);
};

function formatNumber(number, pattern) {
    var str = number.toString();
    var strInt;
    var strFloat;
    var formatInt;
    var formatFloat;
    if (/\./g.test(pattern)) {
        formatInt = pattern.split('.')[0];
        formatFloat = pattern.split('.')[1];
    } else {
        formatInt = pattern;
        formatFloat = null;
    }

    if (/\./g.test(str)) {
        if (formatFloat != null) {
            var tempFloat = Math.round(parseFloat('0.' + str.split('.')[1]) * Math.pow(10, formatFloat.length)) / Math.pow(10, formatFloat.length);
            strInt = (Math.floor(number) + Math.floor(tempFloat)).toString();
            strFloat = /\./g.test(tempFloat.toString()) ? tempFloat.toString().split('.')[1] : '0';
        } else {
            strInt = Math.round(number).toString();
            strFloat = '0';
        }
    } else {
        strInt = str;
        strFloat = '0';
    }
    if (formatInt != null) {
        var outputInt = '';
        var zero = formatInt.match(/0*$/)[0].length;
        var comma = null;
        if (/,/g.test(formatInt)) {
            comma = formatInt.match(/,[^,]*/)[0].length - 1;
        }
        var newReg = new RegExp('(\\d{' + comma + '})', 'g');

        if (strInt.length < zero) {
            outputInt = new Array(zero + 1).join('0') + strInt;
            outputInt = outputInt.substr(outputInt.length - zero, zero)
        } else {
            outputInt = strInt;
        }

        var 
        outputInt = outputInt.substr(0, outputInt.length % comma) + outputInt.substring(outputInt.length % comma).replace(newReg, (comma != null ? ',' : '') + '$1')
        outputInt = outputInt.replace(/^,/, '');

        strInt = outputInt;
    }

    if (formatFloat != null) {
        var outputFloat = '';
        var zero = formatFloat.match(/^0*/)[0].length;

        if (strFloat.length < zero) {
            outputFloat = strFloat + new Array(zero + 1).join('0');
            //outputFloat        = outputFloat.substring(0,formatFloat.length);
            var outputFloat1 = outputFloat.substring(0, zero);
            var outputFloat2 = outputFloat.substring(zero, formatFloat.length);
            outputFloat = outputFloat1 + outputFloat2.replace(/0*$/, '');
        } else {
            outputFloat = strFloat.substring(0, formatFloat.length);
        }

        strFloat = outputFloat;
    } else {
        if (pattern != '' || (pattern == '' && strFloat == '0')) {
            strFloat = '';
        }
    }

    return strInt + (strFloat == '' ? '' : '.' + strFloat);
}

$.fn.calcMaxWidth = function ($arr) {
    var maxWidth = 0;
    $arr.each(function () {
        var w = $(this).width();
        if (w > maxWidth)
            maxWidth = w;
    });
    return maxWidth;
};

$.fn.keyBinding = function ($selector) {
    $(window).unbind('keyup').bind('keyup', function (e) {
        if (e.keyCode == 13)
            $selector.click();
    });
};



function showMessage(message) {
    $.fn.metro.showBottomSide($metro, '<div class="msg_info">' + message + '</div>');
};
function showError(error) {
    $.fn.metro.showBottomSide($metro, '<div class="msg_error">' + error + '</div>');
};
function valEmpty($input, noTrim) {
    if (noTrim)
        return $input.val() != "";
    return $input.val().trim() != "";
};
function valEmptyH($selector, noTrim) {
    if (noTrim)
        return $selector.html() != "";
    return $selector.html().trim() != "";
};
function valEqual($input, val, noTrim) {
    if (noTrim)
        return $input.val() == val;
    return $input.val().trim() == val;
};
function valEqualH($selector, val, noTrim) {
    if (noTrim)
        return $selector.html() == val;
    return $selector.html().trim() == val;
};

function showLoading() {
    adjustOverlay();
    $('.metro_overlay').show();
};
function hideLoading() {
    $('.metro_overlay').hide();
};
function adjustOverlay() {
    var $overlay = $('.metro_overlay');
    var $overlay_content = $('.metro_overlay_content');
    $overlay_content.css({ left: $overlay.width() / 2 - $overlay_content.width() / 2,
        top: $overlay.height() / 2 - $overlay_content.height() / 2
    });
};